﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Entities;

namespace InsuranceVehicle.AdminSite
{
    public partial class UpdateEmployeeInfo : System.Web.UI.Page
    {
            
        protected void Page_Load(object sender, EventArgs e)
        {
            EmployeeInfo emp = new EmployeeInfo();
            emp = (EmployeeInfo)Session["Employee"];
           

            if (emp != null)
            {
                if (emp.Department == 1)
                {
                    Menu1.ShowElement = "all";
                }
                else
                {
                    Menu1.ShowElement = "no";
                }

                if (!IsPostBack)
                {
                    addListItem();
                    txtFirstname.Text = emp.Firstname;
                    txtLastname.Text = emp.Lastname;

                    string dob = emp.Dob.ToString();
                    string[] strArr = dob.Split('/');

                    cbbMonth.SelectedValue = strArr[0];
                    cbbDay.SelectedValue = strArr[1];
                    cbbYear.SelectedValue = strArr[2];

                    txtAddress.Text = emp.Address.ToString();
                    txtPhone.Text = emp.Phone.ToString();
                    txtEmail.Text = emp.Email.ToString();
                }
            }
            else
            {
                Response.Redirect("LoginAdmin.aspx");
            }
        }

        private void addListItem()
        {
            for (int i = 1; i <= 12; i++)
            {
                cbbMonth.Items.Add(i.ToString());
            }

            for (int i = 1; i <= 31; i++)
            {
                cbbDay.Items.Add(i.ToString());
            }

            int curYear = DateTime.Now.Year;
            for (int i = curYear - 80; i <= curYear - 18; i++)
            {
                cbbYear.Items.Add(i.ToString());
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            EmployeeDataAccess db = new EmployeeDataAccess();
            EmployeeInfo emp = new EmployeeInfo();
            emp = (EmployeeInfo)Session["Employee"];
            lblSuccess.Visible = false;
            if (IsValid)
            {

                emp.EmployeeID = emp.EmployeeID;
                emp.Firstname = txtFirstname.Text;
                emp.Lastname = txtLastname.Text;

                emp.Dob = cbbMonth.SelectedItem.ToString() + "/" +
                    cbbDay.SelectedItem.ToString() + "/" + cbbYear.SelectedItem.ToString();
                emp.Address = txtAddress.Text;
                emp.Phone = txtPhone.Text;
                emp.Email = txtEmail.Text;

                db.UpdateInfomation(emp);
                lblSuccess.Visible = true;
                lblSuccess.Text = "Update successfull";
            }
        }
       
    }
}